package ArrayList;

import java.util.ArrayList;
import java.util.Objects;

class Student{
    public int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(age);
    }
}

public class ArrayListDemo3 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Student> list12 = new ArrayList<Student>();

        //排序
        //list12.sort("")

        //lambda
        list.forEach(j->System.out.println(j));

        //=>
        for(String j : list){
            System.out.println(j);
        }

        list.add("chen");
        list.add("chen");
        list.add("zhao");
        list.add("zhang");
        list.add("liu");

        //1、public E get(Int index):获取某个索引位置处的元素值
        String e = list.get(3);
        System.out.println(e);

        //2、public int size():在获取集合的大小（元素个数）
        System.out.println(list.size());

        //3、完成集合的遍历
        for (int i = 0; i < 5; i++) {
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
            System.out.println(list.get(3));
            System.out.println(list.get(4));

            for (int j = 0; j < list.size(); j++) {
                System.out.println(list.get(i));

            }
        }

        //4、public E remove(int index):删除某个索引位置处的元素值，并返回被删除的元素值
        System.out.println(list);
        String e2 = list.remove(2);
        System.out.println(e2);
        System.out.println(list);
        //5、public boolean remove(Object o):直接删除元素值，删除成功返回ture，删除失败返回false
        System.out.println(list.remove("zhang"));
        System.out.println(list);

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("chen");
        list2.add("li");
        list2.add("chen");
        list2.add("liu");
        System.out.println(list2.remove("chen"));
        System.out.println(list2);


        //6、public E set(int index , E element):修改某个索引处的元素值
        String e3 = list.set(2,"liang");
        System.out.println(e3);
        System.out.println(list2);

        //慢 可删除
        for(int i =0;i<list2.size();i++){
            list2.remove("chen");
        }

        //快 不可删除
        for(String str : list2){
            System.out.println(str);
            list2.remove("chen");
        }

    }
}
